package com.ygl.common.nacos.operation;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.Executor;

/**
 * author: yanggld
 * date: 2019/12/15 22:31
 * version: 1.0
 * description: NacosOperation
 */
public class NacosOperation {

    @Autowired
    private ConfigService nacosConfigService;

    @Autowired
    private NamingService nacosNamingService;
    /**
     * 获取nacos配置信息
     * @param group 配置分组
     * @param serviceId 配置 ID
     * @param timeoutMs 读取配置超时时间，单位 ms，推荐值 3000
     * @return
     * @throws NacosException
     */
    public String getConfig(String group, String serviceId, long timeoutMs) throws NacosException {
        return nacosConfigService.getConfig(serviceId, group,timeoutMs);
    }

    /**
     * 删除配置： 用于通过程序自动删除 Nacos 配置，以便通过自动化手段降低运维成本。
     * @param group  配置分组
     * @param serviceId   配置 ID
     * @return 是否删除成功
     * @throws NacosException
     */
    public boolean removeConfig(String group, String serviceId) throws NacosException {
        return nacosConfigService.removeConfig(serviceId, group);
    }

    /**
     * 发布配置：用于通过程序自动发布 Nacos 配置，以便通过自动化手段降低运维成本。
     * @param group  配置分组
     * @param serviceId  配置 ID
     * @param config  配置内容，不超过 100K 字节。
     * @return 是否发布成功
     * @throws NacosException
     */
    public boolean publishConfig(String group, String serviceId, String config) throws NacosException {
        return nacosConfigService.publishConfig(serviceId, group, config);
    }

    /**
     * 删除监听
     * @param group  配置分组
     * @param serviceId 配置 ID
     * @param configListener 监听器
     */
    public void unsubscribeConfig(String group, String serviceId, Listener configListener) {
        nacosConfigService.removeListener(serviceId, group, configListener);
    }

    /**
     * 监听配置
     * @param group 配置分组
     * @param serviceId 配置 ID
     * @param executor
     * @param subscribeCallback
     * @return
     * @throws NacosException
     */
    public Listener subscribeConfig(String group, String serviceId, Executor executor, NacosSubscribeCallback subscribeCallback) throws NacosException {
        Listener configListener = new Listener() {
            @Override
            public void receiveConfigInfo(String config) {
                subscribeCallback.callback(config);
            }

            @Override
            public Executor getExecutor() {
                return executor;
            }
        };

        nacosConfigService.addListener(serviceId, group, configListener);
        return configListener;
    }

    /**
     * 监听服务
     * @param serviceId  服务名
     * @param subscribeCallback
     * @return
     * @throws NacosException
     */
    public EventListener subscribeService(String serviceId,NacosSubscribeCallback subscribeCallback) throws NacosException {
        EventListener eventListener = new EventListener() {
            @Override
            public void onEvent(Event event) {
                subscribeCallback.callback(event);
            }
        };
        nacosNamingService.subscribe(serviceId, eventListener);
        return eventListener;
    }
}
